﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgorApp.Roma.Model;
using AgorApp.Roma.Model.DataTransferObjects;
using Newtonsoft.Json;
using AgorApp.Roma.Model.DataTransferObjects.Utils;

namespace AgorApp.Roma.Oracle.Services.Mappers
{
    public class DataTransferObjectsProfile : Profile
    {
        public override string ProfileName
        {
            get
            {
                return this.GetType().Name;
            }
        }

        protected override void Configure()
        {
            base.Configure();
            SourceMemberNamingConvention = new UpperUnderscoreNamingConvention();
            DestinationMemberNamingConvention = new PascalCaseNamingConvention();

            CreateMap<ASSEMBLEE_CAPITOLINE, AssembleeCapitoline>()
                .ForMember(dest => dest.TestoOdG, opt => opt.ResolveUsing((ASSEMBLEE_CAPITOLINE src) =>
                {
                    return JsonConvert.DeserializeObject<AssembleeCapitoline.TestoObject>(src.TESTO_ODG);
                }))
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));

            CreateMap<CONSIGLIERI_ASSEMBLEA, ConsiglieriAssemblea>()
                .ForMember(dest => dest.Note, opt => opt.ResolveUsing((CONSIGLIERI_ASSEMBLEA src) =>
                {
                    return JsonConvert.DeserializeObject<ConsiglieriAssemblea.NoteObject>(src.NOTE);
                }))
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));

            CreateMap<CONSIGLIERI_IMAGES, ConsiglieriImages>()
                .ForMember(dest => dest.Consigliere, opt => opt.MapFrom<CONSIGLIERI_ASSEMBLEA>(src => src.CONSIGLIERI_ASSEMBLEA))
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));

            CreateMap<DELIBERE_ASSEMBLEA_CAPITOLINA, DelibereAssembleaCapitolina>()
                .ForMember(dest => dest.Testo, opt => opt.ResolveUsing((DELIBERE_ASSEMBLEA_CAPITOLINA src) =>
                {
                    return JsonConvert.DeserializeObject<DelibereAssembleaCapitolina.TestoObj>(src.TESTO);
                }))
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));

            CreateMap<DELIBERE_ASSEMBLEA_FILE, DelibereAssembleaFile>()
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));

            CreateMap<GRUPPI_CONSILIARI, GruppiConsiliari>()
                .ForMember(dest => dest.Note, opt => opt.ResolveUsing((GRUPPI_CONSILIARI src) =>
                {
                    if (!string.IsNullOrWhiteSpace(src.NOTE))
                        return JsonConvert.DeserializeObject<GruppiConsiliari.NoteObj>(src.NOTE);
                    return null;
                }))
                .ForMember(dest => dest.InMaggioranza, opt => opt.ResolveUsing((GRUPPI_CONSILIARI src) =>
                {
                    if (!src.IN_MAGGIORANZA.HasValue) return false;
                    return Convert.ToBoolean(src.IN_MAGGIORANZA);
                }))
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));

            CreateMap<GRUPPI_CONSILIARI_IMAGES, GruppiConsiliariImages>()
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));

            CreateMap<NEWS, News>()
                .ForMember(dest => dest.Testo, opt => opt.ResolveUsing((NEWS src) =>
                {
                    return JsonConvert.DeserializeObject<News.TestoObj>(src.TESTO);
                }))
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));

            CreateMap<NEWS_IMAGES, NewsImages>()
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));

            CreateMap<TAG, Tag>()
                .ForMember(dest => dest.TagName, opt => opt.MapFrom(src => src.TAG1))
                .ForMember(dest => dest.TagType, opt => opt.ResolveUsing((TAG src) =>
                {
                    if (Enum.IsDefined(typeof(TagTypeEnum), src.ID_TIPO_TAG))
                        return (TagTypeEnum)src.ID_TIPO_TAG;
                    return default(TagTypeEnum);
                }))
                .ForMember(dest => dest.ObjectState, opt => opt.UseValue<EntityObjectState>(EntityObjectState.Detached));
        }
    }
}
